package com.yanqu.road.server.manger;

import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.plugin.ManagerReloadListener;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.ServerType;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

public class ChannelConfigMgr extends TempMgr {

	private static Logger logger = LogManager.getLogger(ChannelConfigMgr.class.getName());

	private static List<ManagerReloadListener> managerReloadListener = new ArrayList<>();

	private static int cacheServerType = 0;

	public ChannelConfigMgr(int serverType) {
		cacheServerType = serverType;
	}

	@Override
	public boolean stop() {
		return false;
	}

	@Override
	public boolean init() throws Exception {
		return reload();
	}

	@Override
	public boolean reloadConfig() {
		int channelId = 0;
		if(cacheServerType == ServerType.SERVER_CROSS
				|| cacheServerType == ServerType.SERVER_CROSS_2
				|| cacheServerType == ServerType.SERVER_CHANNEL
				|| cacheServerType == ServerType.CENTER_CROSS_2
				|| cacheServerType == ServerType.BANQUET_CROSS
		){
			channelId = ConfigHelper.getInt("channelId");
		}else {
			long serverId = Long.parseLong(ConfigHelper.getValue("serverId"));
			channelId = ServerListMgr.getChannelId(serverId);
		}

		ChannelConfig.reloadChannelConfig(channelId);
		notifyReload();
		return true;
	}

	@Override
	public boolean reloadData() throws Exception {
		return true;
	}

	public static void addReloadListener(ManagerReloadListener reloadListener) {
		managerReloadListener.add(reloadListener);
	}

	private static void notifyReload() {
		if (null != managerReloadListener) {
			for (ManagerReloadListener listener : managerReloadListener) {
				listener.notifyReload();
			}
		}
	}

//	public static boolean isMainLandChannel(int channelId) {
//		if(331 == channelId){
//			return true;
//		}
//		for (eChannelType type : eChannelType.values()) {
//			if (type.getValue() == channelId) {
//				if (type.getPlaceType() == ePlaceType.MainLand)
//					return true;
//			}
//		}
//		return false;
//
//	}

}
